package com.zimi.controller;


import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.zimi.model.SysOrgExtend;
import com.zimi.model.SysOrgLeader;
import com.zimi.model.SysPostSuperiors;
import com.zimi.service.SysOrgLeaderService;
import com.zimi.service.SysPostPrivilegeService;
import com.zimi.service.SysPostSuperiorsService;
import com.zimi.util.CommonUtil;
import com.zimi.util.GsonUtil;
import io.swagger.annotations.ApiOperation;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestMapping;

import org.springframework.web.bind.annotation.RestController;

import javax.servlet.http.HttpServletRequest;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

/**
 * 岗位功能授权表 api
 * @author lnintr
 * @since 2019-05-22
 */
@RestController
@RequestMapping("sys-post-privilege")
public class SysPostPrivilegeApi {
    @Autowired
    private SysPostPrivilegeService sysPostPrivilegeService;
    @Autowired
    private CommonUtil commonUtil;
    @Autowired
    private SysPostSuperiorsService superiorsService;

    @PostMapping("getOrgAndPost")
    @ApiOperation("查询机构和岗位信息")
    public Object getOrgAndPost(SysOrgExtend record) {
        Map<String, Object> objectMap = new HashMap<>();

        objectMap.put("data", sysPostPrivilegeService.getPostTree(record));
        objectMap.put("code", 0);
        return objectMap;
    }

    @PostMapping("getLeaderList")
    @ApiOperation("查询部门领导信息")
    public Object getLeaderList(SysOrgExtend record) {
        Map<String, Object> objectMap = new HashMap<>();

        List<SysOrgLeader> list = new SysOrgLeader().selectList(new LambdaQueryWrapper<SysOrgLeader>().eq(SysOrgLeader::getOriginOrgId,record.getOrgId()));

        objectMap.put("data", sysPostPrivilegeService.getLeaderList(record));
        objectMap.put("leaderData", list);
        objectMap.put("code", 0);
        return objectMap;
    }

    @PostMapping("getSupPostList")
    @ApiOperation("查询上级岗位列表")
    public Object getSupPostList(SysOrgExtend record) {
        Map<String, Object> objectMap = new HashMap<>();
        List<SysPostSuperiors> superiorsList = new SysPostSuperiors().selectList(new LambdaQueryWrapper<SysPostSuperiors>().eq(SysPostSuperiors::getOriginOrgId, record.getOrgId()).eq(SysPostSuperiors::getPostId, record.getPostId()));

        objectMap.put("data", sysPostPrivilegeService.getSupPostTree(record));
        objectMap.put("supData",superiorsList);
        objectMap.put("code", 0);
        return objectMap;
    }

    @PostMapping("getPostAndUser")
    @ApiOperation("查询岗位人员信息")
    public Object getPostAndUser(SysOrgExtend record) {
        Map<String, Object> objectMap = new HashMap<>();
        objectMap.put("data", sysPostPrivilegeService.getUserTree(record));
        objectMap.put("code", 0);
        return objectMap;
    }


    @PostMapping("postFunctionList")
    @ApiOperation("查询权限和岗位信息")
    public Object selectRoleFunctionList(SysOrgExtend record) {
        Map<String, Object> objectMap = new HashMap<>();
        objectMap.put("List", sysPostPrivilegeService.getFunctionlistByPost(record));
        objectMap.put("code", 0);
        return objectMap;
    }

    @PostMapping("saveFunctionPost")
    @ApiOperation("修改权限和岗位信息")
    public Object saveFunctionRole(HttpServletRequest request) {
        String[] ma = request.getParameterValues("privilageList");

        List<SysOrgExtend> sysOrgExtends = GsonUtil.json2Objs(ma[0], SysOrgExtend.class);

        sysPostPrivilegeService.saveFunctionPost(sysOrgExtends, 1);

        Map<String, Object> objectMap = new HashMap<>();
        objectMap.put("code", 0);
        return objectMap;
    }

    @PostMapping("deleteFunctionPost")
    @ApiOperation("删除权限和岗位信息")
    public Object deleteFunctionRole(HttpServletRequest request) {
        String[] ma = request.getParameterValues("privilageList");

        List<SysOrgExtend> sysOrgExtends = GsonUtil.json2Objs(ma[0], SysOrgExtend.class);

        sysPostPrivilegeService.saveFunctionPost(sysOrgExtends, 2);

        Map<String, Object> objectMap = new HashMap<>();
        objectMap.put("code", 0);
        return objectMap;
    }
}
